// pages/order/order.js
import { storeApi } from '../../api/index';
import {
    alert,
    toast,
    confirm
} from '../../utils/modal';
// 获取app
// var app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    total: 0,
    checkedTab: 0,
    pageParam: {
      page: 1,
      page_size: 20,
    },
    list: [],
    // 1：待开始,2：已取消,3：待完成,4：待支付,5：已支付
    statusMap: {
    '1':"待开始",
    '2':"已取消",
    '3':"待完成",
    '4':"待支付",
    '5':"已支付"
    },
    orderStatus: [
      {
        value: 0,
        label: "全部"
      },
      {
        value: 4,
        label: "待支付"
      },
      {
        value: 1,
        label: "待开始"
      },
      {
        value: 3,
        label: "待完成"
      },
      {
        value: 5,
        label: "已支付"
      },
      {
        value: 2,
        label: "已取消"
      },
    ],
  },

  changeTab(e) {
    if (this.data.checkedTab == e.detail.name) {
        return;
    }
    this.setData({
      checkedTab: e.detail.name,
      pageParam: {
        page: 1,
        page_size: 20,
      }
    },()=>{
       this.getData()
    })
  },

  async getData(){
    try {
      let param = {
        ...this.data.pageParam
      }
      if(this.data.checkedTab){
        param.status = this.data.checkedTab
      }
      let res = res = await storeApi.queryRanstoreReservationOrderList(param)
      this.setData({
        list: res.list || [],
        total: res.total || 0,
      })
      // console.log(this.data.list)
    } catch (e) { 
      console.log(e)
    }
  },
  goOrderDetial(e) {
      // const store_id = e.currentTarget.dataset.store_id
      const no = e.currentTarget.dataset.id
      wx.navigateTo({
          url: `../orderDetail/orderDetail?id=${no}`
      });
  },
  cancelOrder(e) {
      var that = this;
      var id = e.currentTarget.dataset.id;
      console.log(id)
      confirm('确定取消该预约订单么？', function (msg) {
          if (msg) {
            storeApi.updateReservationOrder({
              id : id,
              status: 2
            }).then(resp => {
                 that.getData();
              });
          }
      });
  },
  goHome(){
    wx.reLaunch({
      url: "/pages/home/home",
    });
  },
  toOrderPay(e) {
    var id = e.currentTarget.dataset.id;
    wx.navigateTo({
      url: `/pages/order-pay/order-pay?id=${id}`,
    });
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options?.checkedTab){
      this.setData({
        checkedTab: Number(options.checkedTab)
      }, ()=>{
         this.getData()
      })
      console.log(this.data.checkedTab)
    }else{
      this.getData()
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    const {total, list, pageParam} = this.data
    if(total > list.length){
      this.setData({
        pageParam: {
          ...pageParam,
          page: pageParam.page + 1,
        }
      }, ()=>{
        this.getData();
      })
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {
    return {
      title: '频繁染白发，就找染博士',
      imageUrl:
        'https://ranboss.oss-cn-beijing.aliyuncs.com/ranstore/share.png',
    };
  }
})